package com.zhiyou.app.shop.manager.action;

import java.util.List;

import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.JsonObject;
import com.zhiyou.app.shop.common.Authority;
import com.zhiyou.app.shop.common.BaseAction;
import com.zhiyou.app.shop.common.InnerObject;
import com.zhiyou.app.shop.manager.dao.pojo.GoodsCategory;
import com.zhiyou.app.shop.manager.service.ISysManageService;

public class GoodsCategoryAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private ISysManageService sysManageService;

	private List<GoodsCategory> listCat;
	
	private GoodsCategory cat = new GoodsCategory();
	
	private String ids;
	
	public GoodsCategoryAction() {
		super();
		this.setCurrentPath(getText("shopManage.path.sysmanage.goodsCategory"));
	}
	@Authority(1)
	public String goodsCategoryList(){
		try {
			sysManageService.getCategoryBypage(this.getInitedPage(), null, Order.desc("createDate"));
		} catch (Exception e) {
			this.setErrorMessage(e);
		}
		return SUCCESS;
	}
	@Authority(1)
	public String addGoodsCategory(){
		this.cat = new GoodsCategory();
		cat.setOrderNumber(sysManageService.getGoodsCatMaxOrder());
		return SUCCESS;
	}
	@Authority(1)
	public String editGoodsCategory(){
		this.cat = sysManageService.findCatById(cat.getId());
		return SUCCESS;
	}
	@Authority(1)
	public String saveOrUpdateGoodsCategory(){
		try {
			Validator.validateGoodsCatAdd(cat);
			sysManageService.saveOrUpdateCategory(cat);
		} catch (Exception e) {
			this.setErrorMessage(e);
			return ERROR;
		}
		
		return SUCCESS;
	}
	@Authority(1)
	public String deleteGoodsCategory(){
		this.printJSON(getResponse(), new InnerObject(){
			public void execute(JsonObject json) {
				sysManageService.deleteCats(ids);
			}
		});
		return null;
	}
	
	public List<GoodsCategory> getListCat() {
		return listCat;
	}

	public void setListCat(List<GoodsCategory> listCat) {
		this.listCat = listCat;
	}

	public GoodsCategory getCat() {
		return cat;
	}

	public void setCat(GoodsCategory cat) {
		this.cat = cat;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}
	
}
